package cn.jianyongjie.system.service.impl;

import cn.jianyongjie.basic.service.impl.BaseServiceImpl;
import cn.jianyongjie.system.domain.Menu;
import cn.jianyongjie.system.domain.Role;
import cn.jianyongjie.system.mapper.MenuMapper;
import cn.jianyongjie.system.mapper.RoleMapper;
import cn.jianyongjie.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;


    /*
    * @Title: RoleServiceImpl
    * @Description: 重写新增
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/15 17:28
    * @Parameters: * @param role
    * @Return void
    */
    @Override
    public void save(Role role) {

        // 新增
        roleMapper.save(role);
        // 新增关联数据
        AddAssociatedData(role);

    }

    private void AddAssociatedData(Role role) {

        Long id = role.getId();
        List<Long> menus = role.getMenus();
        List<Long> permissions = role.getPermissions();
        // 删除以前的数据
        roleMapper.removeByRoleMenuId(id);
        roleMapper.removeByRolePermissionId(id);

        // 新增全新的数据
        roleMapper.saveRoleMenu(id,menus);
        roleMapper.saveRolePermission(id,permissions);
    }


    /*
    * @Title: RoleServiceImpl
    * @Description: 重写删除
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/15 17:29
    * @Parameters: * @param id
    * @Return void
    */
    @Override
    public void remove(Long id) {

        AssociationDeletion(id);

        roleMapper.remove(id);
    }

    /*
    * @Title: RoleServiceImpl
    * @Description: 关联删除
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/16 8:47
    * @Parameters: * @param id
    * @Return void
    */
    private void AssociationDeletion(Long id) {
        roleMapper.removeByRoleMenuId(id);
        roleMapper.removeByRolePermissionId(id);
    }


    /*
    * @Title: RoleServiceImpl
    * @Description: 重写修改
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/15 17:29
    * @Parameters: * @param role
    * @Return void
    */
    @Override
    public void update(Role role) {
        AssociationModification(role);
        roleMapper.update(role);
    }

    /*
    * @Title: RoleServiceImpl
    * @Description: 关联修改
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/16 12:40
    * @Parameters: * @param
    * @Return void
    */
    private void AssociationModification(Role role) {
        Long id = role.getId();
//        // 删除以前的数据
//        roleMapper.removeByRoleMenuId(id);
//        roleMapper.removeByRolePermissionId(id);
    }
}
